import uuid

from sqlalchemy import Enum, Integer, DateTime, ForeignKey, String
from sqlalchemy.orm import relationship, Mapped, mapped_column
from datetime import datetime
from enum import Enum as PyEnum

from db.db_base import BaseModel
from apps.vadmin.auth.models import VadminUser

class TaskStatus(PyEnum):
    PENDING = "pending"
    IN_PROGRESS = "in_progress"
    COMPLETED = "completed"
    FAILED = "failed"

class TaskDB(BaseModel):
    __tablename__ = 'crewplus_tasks'
    __table_args__ = ({'comment': 'Task management for longrun jobs, e.g. document ingestion'})
    id: Mapped[str] = mapped_column(String(255), primary_key=True, index=True, unique = True, default=lambda: str(uuid.uuid4()),comment="task id")
    name: Mapped[str] = mapped_column(String(500), nullable=True, comment="Schema name")

    status: Mapped[TaskStatus] = mapped_column(Enum(TaskStatus), default=TaskStatus.PENDING, comment="Task status")
    
    start_time: Mapped[datetime | None] = mapped_column(DateTime, nullable=True, comment="Task start time")
    end_time: Mapped[datetime | None] = mapped_column(DateTime, nullable=True, comment="Task end time")

    create_user_id: Mapped[int] = mapped_column(
        Integer,
        ForeignKey("vadmin_auth_user.id", ondelete='RESTRICT'),
        comment="Creator"
    )
    create_user: Mapped[VadminUser] = relationship(foreign_keys=create_user_id) 